package com.pro.admin.web.guanwang;


import com.baidu.unbiz.fluentvalidator.ComplexResult;
import com.baidu.unbiz.fluentvalidator.FluentValidator;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pro.admin.annotation.RequiresPermissionsDesc;
import com.pro.admin.web.BaseController;
import com.pro.core.util.ResponseUtil;
import com.pro.core.validator.NotNullValidator;
import com.pro.db.domain.BbsSite;
import com.pro.db.service.itf.IBbsSiteService;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

import static com.baidu.unbiz.fluentvalidator.ResultCollectors.toComplex;



/**
 * <p>
 * 站点表 前端控制器
 * </p>
 *
 * @author 王雷
 * @since 2020-01-07
 */
@RestController
@RequestMapping("/admin/bbsSite")
public class BbsSiteController extends BaseController {
    private final Log logger = LogFactory.getLog(BbsSiteController.class);

    @Autowired
    private IBbsSiteService bbsSiteService;

    /**
     * @apiDefine bbsSite 站点接口
     *
     */

    /**
     * @api {GET} /admin/bbsSite/list 6.1 获取站点列表
     * @apiName bbsSite list
     * @apiGroup bbsSite
     * @apiVersion 1.0.0
     * @apiDescription 接口负责人-王雷
     *
     * @apiParam {int} [page] 页码
     * @apiParam {int} [size] 每页数量
     *
     * @apiSuccess {int} errno 结果码 0:正常返回,-1服务异常
     * @apiSuccess {String} errmsg 前端提示消息
     * @apiSuccess {Object} data 结果集
     * @apiSuccessExample Success-Response:
     * HTTP/1.1 200 OK
    {
     *     "errno": 0,
     *     "errmsg": "成功"
     * }
     */
    @RequiresPermissions("admin:bbsSite:list")
    @RequiresPermissionsDesc(menu={"站点管理" , "站点信息"}, button="站点列表")
    @GetMapping("/list")
    public Object list(@RequestParam(defaultValue = "1") Integer page,
                             @RequestParam(defaultValue = "10") Integer size) {
        Page<BbsSite> pageVo = new Page<>();
        pageVo.setCurrent(page);
        pageVo.setSize(size);
        IPage<BbsSite> pList = bbsSiteService.page(pageVo,
                new QueryWrapper<BbsSite>()
                        .eq("deleted",0)
                        .orderByDesc("add_time"));

        Map<String, Object> data = new HashMap<>();
        data.put("total", pList.getTotal());
        data.put("items", pList.getRecords());
        return ResponseUtil.ok(data);
    }


    /**
     * @api {GET} /admin/bbsSite/read 6.2 站点详情
     * @apiName bbsSite read
     * @apiGroup bbsSite
     * @apiVersion 1.0.0
     * @apiDescription 接口负责人-王雷
     *
     * @apiParam {int} id 站点id
     *
     * @apiSuccess {int} errno 结果码 0:正常返回,-1服务异常
     * @apiSuccess {String} errmsg 前端提示消息
     * @apiSuccess {Object} data 结果集
     * @apiSuccessExample Success-Response:
     * HTTP/1.1 200 OK
    {
     *     "errno": 0,
     *     "errmsg": "成功"
     * }
     */
    @RequiresPermissions("admin:bbsSite:read")
    @RequiresPermissionsDesc(menu={"站点管理" , "站点信息"}, button="站点详情")
    @GetMapping("/read")
    public Object read(@NotNull Integer id) {
        BbsSite entity = bbsSiteService.getById(id);
        return ResponseUtil.ok(entity);
    }

    /**
     * @api {POST} /admin/bbsSite/create 6.3 新增站点
     * @apiName bbsSite create
     * @apiGroup bbsSite
     * @apiVersion 1.0.0
     * @apiDescription 接口负责人-王雷
     *
     * @apiParam {String} name 分站名称
     * @apiParam {String} description 分站描述
     * @apiParam {String} uiType 外观风格，3选1
     * @apiParam {String} [coverUrl] 封面图片
     * @apiParam {String} [iconUrl] 图标
     * @apiParam {String} shortLink 访问短链接
     * @apiParam {String} [label] 标签
     * @apiParam {String} [keywords] 关键字
     * @apiParam {String} [remark] 备注
     * @apiParam {int} [pushStatus] 是否展示（默认展示1，不展示0）
     * @apiParam {int} [pushSort] 展示顺序（越大越靠前，默认最大10000）
     * @apiParam {int} [hotFlag] 是否热门（默认非热门0，热门1）
     *
     * @apiSuccess {int} errno 结果码 0:正常返回,-1服务异常
     * @apiSuccess {String} errmsg 前端提示消息
     * @apiSuccess {Object} data 结果集
     * @apiSuccessExample Success-Response:
     * HTTP/1.1 200 OK
    {
     *     "errno": 0,
     *     "errmsg": "成功"
     * }
     */
    @RequiresPermissions("admin:bbsSite:create")
    @RequiresPermissionsDesc(menu={"站点管理" , "站点信息"}, button="新增站点")
    @PostMapping("/create")
    public Object create(@RequestBody BbsSite entity) {
        ComplexResult check = FluentValidator.checkAll()
                .on(entity, new NotNullValidator("站点"))
                .on(entity.getName(), new NotNullValidator("名字"))
                .on(entity.getDescription(), new NotNullValidator("描述"))
                .on(entity.getUiType(), new NotNullValidator("显示风格"))
                .on(entity.getShortLink(), new NotNullValidator("短链接"))
                .doValidate()
                .result(toComplex());
        if (!check.isSuccess()) {
            //参数校验不通过，直接返回结果并提示缺少哪些参数
            return ResponseUtil.fail(check.getErrors());
        }

        entity.setAddTime(LocalDateTime.now());
        entity.setAdminId(getCurrentUser().getId());
        entity.setAdminName(getCurrentUser().getUsername());
        entity.setDeleted(false);
        bbsSiteService.save(entity);
        return ResponseUtil.ok();
    }

    /**
     * @api {POST} /admin/bbsSite/update 6.4 更新站点
     * @apiName  bbsSite update
     * @apiGroup  bbsSite
     * @apiVersion 1.0.0
     * @apiDescription 接口负责人-王雷
     *
     * @apiParam {int} id 站点id
     * @apiParam {String} name 分站名称
     * @apiParam {String} description 分站描述
     * @apiParam {String} uiType 外观风格，3选1
     * @apiParam {String} [coverUrl] 封面图片
     * @apiParam {String} [iconUrl] 图标
     * @apiParam {String} shortLink 访问短链接
     * @apiParam {String} [label] 标签
     * @apiParam {String} [keywords] 关键字
     * @apiParam {String} [remark] 备注
     * @apiParam {int} [pushStatus] 是否展示（默认展示1，不展示0）
     * @apiParam {int} [pushSort] 展示顺序（越大越靠前，默认最大10000）
     * @apiParam {int} [hotFlag] 是否热门（默认非热门0，热门1）
     *
     * @apiSuccess {int} errno 结果码 0:正常返回,-1服务异常
     * @apiSuccess {String} errmsg 前端提示消息
     * @apiSuccess {Object} data 结果集
     * @apiSuccessExample Success-Response:
     * HTTP/1.1 200 OK
    {
     *     "errno": 0,
     *     "errmsg": "成功"
     * }
     */
    @RequiresPermissions("admin:bbsSite:update")
    @RequiresPermissionsDesc(menu={"站点管理" , "站点信息"}, button="更新站点")
    @PostMapping("/update")
    public Object update(@RequestBody  BbsSite entity) {
        ComplexResult check = FluentValidator.checkAll()
                .on(entity, new NotNullValidator("站点"))
                .on(entity.getId(), new NotNullValidator("站点ID"))
                .doValidate()
                .result(toComplex());
        if (!check.isSuccess()) {
            //参数校验不通过，直接返回结果并提示缺少哪些参数
            return ResponseUtil.fail(check.getErrors());
        }

        entity.setAdminId(getCurrentUser().getId());
        entity.setAdminName(getCurrentUser().getUsername());
        entity.setUpdateTime(LocalDateTime.now());
        bbsSiteService.updateById(entity);
        return ResponseUtil.ok();
    }

    /**
     * @api {POST} /admin/bbsSite/delete 6.5 删除站点
     * @apiName bbsSite delete
     * @apiGroup bbsSite
     * @apiVersion 1.0.0
     * @apiDescription 接口负责人-王雷
     *
     * @apiParam {int} id 站点id
     *
     * @apiSuccess {int} errno 结果码 0:正常返回,-1服务异常
     * @apiSuccess {String} errmsg 前端提示消息
     * @apiSuccess {Object} data 结果集
     * @apiSuccessExample Success-Response:
     * HTTP/1.1 200 OK
    {
     *     "errno": 0,
     *     "errmsg": "成功"
     * }
     */
    @RequiresPermissions("admin:bbsSite:delete")
    @RequiresPermissionsDesc(menu={"站点管理" , "站点信息"}, button="删除站点")
    @PostMapping("/delete")
    public Object delete(@RequestBody BbsSite entity) {
        Integer id = entity.getId();
        if (id == null) {
            return ResponseUtil.fail("ID不能为空");
        }
        bbsSiteService.removeById(id);
        return ResponseUtil.ok();
    }

}

